package com.example.administrator.myapplication.media;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

/**
 * Created by Administrator on 2016/3/24 0024.
 */
public class Sound {
    private Sound(){};
    private static SoundPool mSoundPool;
    private static int[] SOUND_IDS;
    //初始化
    public static void init(Context context,int[] resIds){
        /*
         * 参一：池里最多放多少种音效
         * 参二：音效的种类
         * 参三：音效的质量，暂时无效，给0
         */
        mSoundPool = new SoundPool(resIds.length, AudioManager.STREAM_MUSIC, 0);

        /*
         * 将声音文件的资源id转换为声音id
         */
        SOUND_IDS = new int[resIds.length];
        for (int i = 0; i < resIds.length; i++)
        {
            SOUND_IDS[i] = mSoundPool.load(context, resIds[i], 1);
        }
    }
    //播放
    public static void play(int soundIndex){
        if(soundIndex>=SOUND_IDS.length){
            return;
        }
        /*
         * 参一：声音ID
         * 参二参三：左声道  右声道
         * 参四：优先级
         * 参五：是否循环播放
         * 参六：播放的频率
         */
        mSoundPool.play(SOUND_IDS[soundIndex],1.0f,1.0f,1,0,1.0f);
    }
}
